package org.tmsps.ne4spring.utils.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class YearMonthUtil {

	private static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");

	/**
	 * .获取本月
	 * 
	 * @param yearMonth
	 * @return
	 */
	public static String getYearMonth() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		String preYearMonth = sf.format(cal.getTime());
		return preYearMonth;
	}

	/**
	 * .获取上个月
	 * 
	 * @param yearMonth
	 * @return
	 */
	public static String getPreYearMonth(String yearMonth) {
		try {
			Date date = sf.parse(yearMonth);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MONTH, -1);
			String preYearMonth = sf.format(cal.getTime());
			return preYearMonth;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

}
